var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
// pages/ucenter/auth/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    auth_photoes1: "",
    idCard_z: "",
    isGTH: false,
     // tab切换 
    currentTab: 0,
    // 页面配置  
    winWidth: 0,
    winHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      auth:options.auth
    });
    // 获取系统信息 
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        });
      }
    });
    this.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getData: function () {
    var that = this;
    var data = { token: wx.getStorageSync("token") }
    util.request(api.UserGetAuth, data).then(function (res) {
      if (res.code == "200") {
        var data = {};
        if (res.datas&&res.datas.length>1){
          that.setData({
            currentTab:1
          })
        }
        for (var i = 0; i < res.datas.length; i++) {
          data["" + res.datas[i].code] = res.datas[i].path;
          data["" + res.datas[i].code + "_ok"] = true;
        }
        that.setData(data);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {

  },
  fnChoose: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        if ("idCard_z" == id) {
          that.setData({
            idCard_z: res.tempFilePaths[0],
            idCard_z_ok: false
          })
        } else if ("idCard_f" == id) {
          that.setData({
            idCard_f: res.tempFilePaths[0],
            idCard_f_ok: false
          })
        } else if ("auth_photoes1" == id) {
          that.setData({
            auth_photoes1: res.tempFilePaths[0],
            auth_photoes1_ok: false
          })
        } else if ("auth_photoes2" == id) {
          that.setData({
            auth_photoes2: res.tempFilePaths[0],
            auth_photoes2_ok: false
          })
        } else {
          that.setData({
            auth_photoes3: res.tempFilePaths[0],
            auth_photoes3_ok: false
          })
        }


      }
    })
  },
  upload: function (e) {
    var that = this;
    if (that.data.idCard_z == "") {
      wx.showToast({
        title: '未上传身份证！',
        image: '/static/images/icon_error.png'
      });
      return;
    }
    if (that.data.auth_photoes1 == "" && that.data.isGTH) {
      wx.showToast({
        title: '未上传营业执照！',
        image: '/static/images/icon_error.png'
      });
      return;
    }
    wx.showLoading({
      title: '正在上传...',
      mask: true
    });
    var flag = false;
    var filePath = that.data.idCard_z;
    var fileName = "身份证正面照";
    var id = "idCard_z";
    wx.uploadFile({
      url: api.UserAuth,
      filePath: filePath,
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'session_token': wx.getStorageSync('session_token'),
        "token": wx.getStorageSync("token"),
        "name": fileName,
        "code": id,
        "authType": that.data.currentTab
      },
      success: function (res) {
        wx.hideLoading();
        if (res.statusCode != 200) {
          flag = true;
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        } else if (JSON.parse(res.data).code != '200') {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          flag = true;
          return;
        } else {
        //上传成功，开始上传第二张
          if (that.data.isGTH){
            filePath = that.data.auth_photoes1;
            fileName = '营业执照';
            id = "auth_photoes1";
            wx.uploadFile({
              url: api.UserAuth,
              filePath: filePath,
              name: 'file',
              header: { "Content-Type": "multipart/form-data" },
              formData: {
                //和服务器约定的token, 一般也可以放在header中
                'session_token': wx.getStorageSync('session_token'),
                "token": wx.getStorageSync("token"),
                "name": fileName,
                "code": id
              },
              success: function (res) {
                wx.hideLoading();
                if (res.statusCode != 200) {
                  flag = true;
                  wx.showModal({
                    title: '提示',
                    content: '上传失败',
                    showCancel: false
                  })
                  return;
                } else if (JSON.parse(res.data).code != '200') {
                  wx.showModal({
                    title: '提示',
                    content: '上传失败',
                    showCancel: false
                  })
                  flag = true;
                  return;
                } else {
                 wx.showToast({
                   title: '上传成功!',
                 });
                 setTimeout(function () {
                   wx.navigateBack({
                   })
                 }, 2000)
                }
              },
              fail: function (e) {
                wx.showModal({
                  title: '提示',
                  content: '上传失败',
                  showCancel: false
                })
                flag = true;
              },
              complete: function () {
              }
            });
          }else{
            wx.showToast({
              title: '上传成功!',
            });
            setTimeout(function () {
              wx.navigateBack({
              })
            }, 2000)
          }
        }
      },
      fail: function (e) {
        console.log(e);
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
        flag = true;
      },
      complete: function () {
      }
    });
    
  },

  // 滑动切换tab 
  bindChange: function (e) {
    var that = this;
    var currentTab = e.detail.current;
    //个人
    if("0"==currentTab){
      that.setData({ 
        currentTab: currentTab,
        auth_photoes1: "",
        idCard_z: "",
        isGTH: false
       });
    }else{
      that.setData({
        currentTab: currentTab,
        auth_photoes1: "",
        idCard_z: "",
        isGTH: true
      });
    }
  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  back:function(){
    wx.navigateBack({
      
    })
  },
  confirm:function(){
    this.setData({
      showModal:true
    })
  }
})